#include "tf_Node.h"

TfNode::TfNode()
{
	m_ver = 1;
}

TfNode::TfNode(TfPoint const& pt)
{
	m_ver = 1;
	m_isValid = true;
	m_pt = pt;
	m_id = 0;

}

TfNode::TfNode(double pt[3])
{
	m_ver = 1;
	m_isValid = true;
	m_pt.SetPoint(pt);
	m_id = 0;	
	
}

void TfNode::SetPoint(double pt[3])
{
	m_pt.SetPoint(pt);
}

void TfNode::SetPoint(TfPoint& pt)
{
	m_pt = pt;
}

TfNode::~TfNode()
{

}


TfPoint const& TfNode::GetPoint() const
{
	return m_pt;
}

void TfNode::GetPoint(double pt[3]) const
{
	double dpt[3];

	m_pt.GetPoint(dpt);
	pt[0] = dpt[0];
	pt[1] = dpt[1];
	pt[2] = dpt[2];
}

TfNode& TfNode::operator=(TfNode const& rhs)
{
	m_pt = rhs.GetPoint();
	m_ver = rhs.m_ver;
	m_id  = rhs.m_id;
	m_partId = rhs.m_partId;
	m_isValid = rhs.m_isValid;
	return *this;
}

/************************************************************************/
/*                                                                      */
/************************************************************************/

TfNodeManager::TfNodeManager()
{
	AdditionSpace();
	m_ver = 1;
}

TfNodeManager::~TfNodeManager()
{
	int i;
	for(i = 0; i < m_data.size(); i++)
	{
		delete m_data[i];
		m_data[i] = NULL;
	}
	m_data.clear();
}

TfNodeManager* TfNodeManager::Instance()
{
	static TfNodeManager ndManager;
	return &ndManager;
}

int TfNodeManager::CreateNode(TfPoint& pt)
{
	TfNode nd;
	
	nd.SetPoint(pt);
	nd.m_id = GetMaxId()+1;
	nd.m_isValid = true;
	
	int idx;
	idx = End();
	AddToDataBase(idx+1,&nd);

	return nd.m_id;
}

int TfNodeManager::CreateNode(double pt[3])
{
	TfNode nd;

	nd.SetPoint(pt);
	nd.m_id = GetMaxId()+1;
	nd.m_isValid = true;
	
	int idx;
	idx = End();
	AddToDataBase(idx+1,&nd);

	return nd.m_id;

}

void TfNodeManager::DeleteNode(int id)
{
	int idx;
	
	idx = (*this)[id];
	if(idx > -1)
	{
		m_data[idx]->Delete();
	}
}

void TfNodeManager::DeleteNodes(IntVector& ids)
{
	int i;
	for(i = 0; i < ids.size(); i++)
		DeleteNode(ids[i]);
}


void TfNodeManager::GetNodePoint(int id,double pt[3])
{
	int idx;
	
	idx = (*this)[id];
	if(idx > -1)
	{
		TfNode* pNd = (TfNode*)(m_data[idx]);
		pNd->GetPoint(pt);
	}
}
void TfNodeManager::AddToDataBase(int idx, TfGeomObject* obj)
{
	if(idx > -1)
	{
		TfNode* pNd = (TfNode*)(m_data[idx]);
		TfNode* pObj = (TfNode*)obj;
		*pNd = *pObj;

		AddToMap(obj->GetId(),idx);
	}
}

void TfNodeManager::AdditionSpace()
{
	int i;
	TfNode* pObj;
	for(i = 0; i < 10000; i++)
	{
		pObj = new TfNode;
		m_data.push_back(pObj);
	}
}
